package com.daybook.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.daybook.mapper.BooksMapper;
import com.daybook.mapper.MemberMapper;
import com.daybook.mapper.OrderMapper;
import com.daybook.mapper.ShopCartMapper;
import com.daybook.mapper.UserMapper;
import com.daybook.pojo.BNumber;
import com.daybook.pojo.Books;
import com.daybook.pojo.Category;
import com.daybook.pojo.Member;
import com.daybook.pojo.Order;
import com.daybook.pojo.OrderAndBooks;
import com.daybook.pojo.User;
import com.daybook.service.OrderService;
import com.daybook.util.PageHelp;
@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ShopCartMapper shopCartMapper;
	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private BooksMapper booksMapper;
	//事务执行
	@Transactional 
	@Override
	public int addNewOrderList(List<Order> orderList,Integer[] sid_list,double payMoney) {
		int result=-1;
		 try {  
			 /**********添加订单***********/
			 result=orderMapper.insertNewOrderList(orderList);
			 /*********清空购物车***********/
			 shopCartMapper.deleteCartByListId(sid_list);
			 /*************更新用户积分**********************/
			 //或许多少钱换一积分信息
			 Member member=memberMapper.selectMemberByMid();
			 int addIntgral=(int)(payMoney/member.getMdiscount());
			 User user=new User();
			 user.setUid(orderList.get(0).getUid());
			 user.setUintegral(addIntgral);
			 userMapper.updateUserByIntegral(user);
			 //更新书的数量
			 List<BNumber> bList=new ArrayList<>();
			 for(int i=0;i<orderList.size();i++){
				 BNumber bNumber=new BNumber();
				 bNumber.setBid(orderList.get(i).getBid());
				 bNumber.setNumber(orderList.get(i).getOnumber());
				 bList.add(bNumber);
			 }
			 booksMapper.updateBookByNumber(bList);
	          
	        } catch (Exception e) {  
	           
	            e.printStackTrace();  
	            throw new RuntimeException("事务回滚，未执行成功！");  
	        }  
		return result;
	}

	@Override
	public int changeOrderState(Order order) {
		// TODO Auto-generated method stub
		return orderMapper.updateOrderState(order);
	}
	/**用户获取订单列表
	 * pageNum位每页数量
	 * */	
	public PageHelp getOrderPageList(Integer pageNow,Integer uid,Integer ostate,Integer pageNum) {
		   if(ostate==null||ostate==0){
		    	ostate=null;
		    }
	    int totalCount=orderMapper.selectCountMyOrderList(uid, ostate);//获取总的记录数
	    List<OrderAndBooks> orderList=new ArrayList<OrderAndBooks>();
	    PageHelp page=null;
	    if(pageNum==null){
	    	pageNum=5;
	    }
	 
	    if(pageNow!=null){
	        page=new PageHelp(totalCount, pageNow);
	        page.setPageSize(pageNum);
	        orderList=orderMapper.selectMyOrderList(page.getStartPos(),page.getPageSize(), uid, ostate);//从startPos开始，获取pageSize条数据
	    }else{
	        page=new PageHelp(totalCount, 1);//初始化pageNow为1
	        page.setPageSize(pageNum);
	        orderList=orderMapper.selectMyOrderList(page.getStartPos(), 5, uid, ostate);//从startPos开始，获取pageSize条数据
	    }
	    page.setList(orderList);
	    return page;
	}
	
	/*********************管理员模块****************************/
	public PageHelp getAllOrderList(Map<String, Object> map,Integer pageNow){
		int totalCount=orderMapper.selectCountAllOrderList(map);//获取总的记录数
		 List<OrderAndBooks> ordersList=new ArrayList<OrderAndBooks>();
		    PageHelp page=null;
		    if(pageNow!=null){
		        page=new PageHelp(totalCount, pageNow);
		        page.setPageSize(8);
		        map.put("startPos", page.getStartPos());
		        map.put("pageSize", page.getPageSize());
		        ordersList=orderMapper.selectAllOrderList(map);//从startPos开始，获取pageSize条数据
		    }else{
		        page=new PageHelp(totalCount, 1);//初始化pageNow为1
		        page.setPageSize(8);
		        map.put("startPos", page.getStartPos());
		        map.put("pageSize", page.getPageSize());
		        ordersList=orderMapper.selectAllOrderList(map);//从startPos开始，获取pageSize条数据
		    }
		    page.setList(ordersList);
		    return page;
	}
}
